<template>
  <div class="orders-page">
    <div class="page-header">
      <h2>订单管理</h2>
      <div class="header-actions">
        <button class="btn-secondary">导出数据</button>
        <button class="btn-primary">新建订单</button>
      </div>
    </div>
    
    <div class="stats-bar">
      <div class="stat-item">
        <span class="stat-label">总订单数</span>
        <span class="stat-value">{{ orders.length }}</span>
      </div>
      <div class="stat-item">
        <span class="stat-label">待处理</span>
        <span class="stat-value">{{ pendingOrders }}</span>
      </div>
      <div class="stat-item">
        <span class="stat-label">已完成</span>
        <span class="stat-value">{{ completedOrders }}</span>
      </div>
      <div class="stat-item">
        <span class="stat-label">总金额</span>
        <span class="stat-value">¥{{ totalAmount }}</span>
      </div>
    </div>

    <div class="filters-bar">
      <div class="search-box">
        <input type="text" placeholder="搜索订单..." v-model="searchQuery">
      </div>
      <div class="filter-buttons">
        <button class="filter-btn active">全部</button>
        <button class="filter-btn">待付款</button>
        <button class="filter-btn">已付款</button>
        <button class="filter-btn">配送中</button>
        <button class="filter-btn">已完成</button>
      </div>
    </div>

    <div class="orders-table">
      <table>
        <thead>
          <tr>
            <th>订单号</th>
            <th>用户</th>
            <th>商品</th>
            <th>数量</th>
            <th>金额</th>
            <th>状态</th>
            <th>下单时间</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="order in filteredOrders" :key="order.id">
            <td>{{ order.orderNumber }}</td>
            <td>
              <div class="user-info">
                <div class="user-avatar">{{ order.user.charAt(0) }}</div>
                <span>{{ order.user }}</span>
              </div>
            </td>
            <td>{{ order.product }}</td>
            <td>{{ order.quantity }}</td>
            <td class="amount">¥{{ order.amount }}</td>
            <td>
              <span class="status-badge" :class="order.status">{{ getStatusText(order.status) }}</span>
            </td>
            <td>{{ order.createdAt }}</td>
            <td>
              <div class="action-buttons">
                <button class="btn-action view">查看</button>
                <button class="btn-action edit">编辑</button>
              </div>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'

const searchQuery = ref('')

const orders = ref([
  { id: 1, orderNumber: 'ORD001', user: '张三', product: '无线蓝牙耳机', quantity: 1, amount: 199, status: 'completed', createdAt: '2024-01-15 10:30' },
  { id: 2, orderNumber: 'ORD002', user: '李四', product: '智能手表', quantity: 2, amount: 1798, status: 'pending', createdAt: '2024-01-16 14:20' },
  { id: 3, orderNumber: 'ORD003', user: '王五', product: '便携充电宝', quantity: 1, amount: 89, status: 'shipped', createdAt: '2024-01-17 09:15' },
  { id: 4, orderNumber: 'ORD004', user: '赵六', product: '无线充电器', quantity: 3, amount: 447, status: 'paid', createdAt: '2024-01-18 16:45' },
  { id: 5, orderNumber: 'ORD005', user: '钱七', product: '蓝牙音响', quantity: 1, amount: 299, status: 'completed', createdAt: '2024-01-19 11:20' },
  { id: 6, orderNumber: 'ORD006', user: '孙八', product: '智能手环', quantity: 2, amount: 318, status: 'pending', createdAt: '2024-01-20 13:10' },
])

const filteredOrders = computed(() => {
  if (!searchQuery.value) return orders.value
  return orders.value.filter(order => 
    order.orderNumber.toLowerCase().includes(searchQuery.value.toLowerCase()) ||
    order.user.toLowerCase().includes(searchQuery.value.toLowerCase()) ||
    order.product.toLowerCase().includes(searchQuery.value.toLowerCase())
  )
})

const pendingOrders = computed(() => {
  return orders.value.filter(order => order.status === 'pending').length
})

const completedOrders = computed(() => {
  return orders.value.filter(order => order.status === 'completed').length
})

const totalAmount = computed(() => {
  return orders.value.reduce((total, order) => total + order.amount, 0)
})

const getStatusText = (status) => {
  const statusMap = {
    pending: '待付款',
    paid: '已付款',
    shipped: '配送中',
    completed: '已完成'
  }
  return statusMap[status] || status
}
</script>

<style scoped>
.orders-page {
  animation: fadeIn 0.3s ease-in-out;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
}

.page-header h2 {
  color: #2c3e50;
  margin: 0;
  font-size: 28px;
}

.header-actions {
  display: flex;
  gap: 12px;
}

.btn-primary {
  background: #3498db;
  color: white;
  border: none;
  padding: 12px 24px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 500;
  transition: background 0.2s ease;
}

.btn-primary:hover {
  background: #2980b9;
}

.btn-secondary {
  background: #95a5a6;
  color: white;
  border: none;
  padding: 12px 24px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 500;
  transition: background 0.2s ease;
}

.btn-secondary:hover {
  background: #7f8c8d;
}

.stats-bar {
  display: flex;
  gap: 20px;
  margin-bottom: 30px;
}

.stat-item {
  background: white;
  padding: 20px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  flex: 1;
  text-align: center;
}

.stat-label {
  display: block;
  font-size: 14px;
  color: #7f8c8d;
  margin-bottom: 8px;
}

.stat-value {
  font-size: 24px;
  font-weight: 700;
  color: #2c3e50;
}

.filters-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  background: white;
  padding: 20px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.search-box input {
  width: 300px;
  padding: 12px 16px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 14px;
}

.filter-buttons {
  display: flex;
  gap: 8px;
}

.filter-btn {
  padding: 8px 16px;
  border: 1px solid #ddd;
  background: white;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.filter-btn.active {
  background: #3498db;
  color: white;
  border-color: #3498db;
}

.orders-table {
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th, td {
  padding: 16px;
  text-align: left;
  border-bottom: 1px solid #f0f0f0;
}

th {
  background: #f8f9fa;
  font-weight: 600;
  color: #2c3e50;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.user-avatar {
  width: 32px;
  height: 32px;
  background: #3498db;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: 600;
  font-size: 14px;
}

.amount {
  font-weight: 600;
  color: #e74c3c;
}

.status-badge {
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

.status-badge.pending {
  background: #fef5e7;
  color: #f39c12;
}

.status-badge.paid {
  background: #eaf4fd;
  color: #3498db;
}

.status-badge.shipped {
  background: #e8f5e8;
  color: #27ae60;
}

.status-badge.completed {
  background: #d5f5e3;
  color: #27ae60;
}

.action-buttons {
  display: flex;
  gap: 8px;
}

.btn-action {
  padding: 6px 12px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  transition: all 0.2s ease;
}

.btn-action.view {
  background: #27ae60;
  color: white;
}

.btn-action.edit {
  background: #3498db;
  color: white;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 768px) {
  .stats-bar {
    flex-direction: column;
  }
  
  .filters-bar {
    flex-direction: column;
    gap: 16px;
  }
  
  .search-box input {
    width: 100%;
  }
  
  .header-actions {
    flex-direction: column;
    gap: 8px;
  }
}
</style> 